package com.athena.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.athena.common.utils.StringUtils;
import com.athena.common.core.page.TableDataInfo;
import com.athena.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.athena.crm.domain.bo.TUnlockRecordBo;
import com.athena.crm.domain.vo.TUnlockRecordVo;
import com.athena.crm.domain.TUnlockRecord;
import com.athena.crm.mapper.TUnlockRecordMapper;
import com.athena.crm.service.ITUnlockRecordService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户解锁企业记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-20
 */
@RequiredArgsConstructor
@Service
public class TUnlockRecordServiceImpl implements ITUnlockRecordService {

    private final TUnlockRecordMapper baseMapper;

    /**
     * 查询用户解锁企业记录
     */
    @Override
    public TUnlockRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户解锁企业记录列表
     */
    @Override
    public TableDataInfo<TUnlockRecordVo> queryPageList(TUnlockRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TUnlockRecord> lqw = buildQueryWrapper(bo);
        Page<TUnlockRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户解锁企业记录列表
     */
    @Override
    public List<TUnlockRecordVo> queryList(TUnlockRecordBo bo) {
        LambdaQueryWrapper<TUnlockRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TUnlockRecord> buildQueryWrapper(TUnlockRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TUnlockRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, TUnlockRecord::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), TUnlockRecord::getBatchNo, bo.getBatchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getComId()), TUnlockRecord::getComId, bo.getComId());
        lqw.eq(bo.getIsDown() != null, TUnlockRecord::getIsDown, bo.getIsDown());
        lqw.eq(bo.getDownTime() != null, TUnlockRecord::getDownTime, bo.getDownTime());
        return lqw;
    }

    /**
     * 新增用户解锁企业记录
     */
    @Override
    public Boolean insertByBo(TUnlockRecordBo bo, Long userId) {
        TUnlockRecord add = BeanUtil.toBean(bo, TUnlockRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean batchInsertByBo(List<TUnlockRecordBo> bo) {
        List<TUnlockRecord> tUnlockRecords = new ArrayList<>();
        for (TUnlockRecordBo tUnlockRecordBo : bo) {
            TUnlockRecord add = BeanUtil.toBean(tUnlockRecordBo, TUnlockRecord.class);
            validEntityBeforeSave(add);
            tUnlockRecords.add(add);
        }
        boolean flag = baseMapper.insertBatch(tUnlockRecords);
        return flag;
    }

    /**
     * 修改用户解锁企业记录
     */
    @Override
    public Boolean updateByBo(TUnlockRecordBo bo) {
        TUnlockRecord update = BeanUtil.toBean(bo, TUnlockRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TUnlockRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户解锁企业记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
